using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using ZWFrameWork;

public enum MJGroup
{
    None = 0,
    
    Tiao = 1,       //1-9
    Bin = 2,        //1-9
    Wan = 3,        //1-9
    Feng = 4,       //东南西北中发白
    Hua = 5,        //春夏秋冬梅兰竹菊
    
    Max = 5,
}

public class MJCtrl : SingletonInstance<MJCtrl>
{

    private GameObject mMJObj;
    private Dictionary<int,Sprite> mMjCardSpriteDic = new Dictionary<int, Sprite>();
    private Dictionary<int,List<MJCS>> mMJCardDic = new Dictionary<int,List<MJCS>>();
    private List<MJCS> mShowMjList = new List<MJCS>();
    private Dictionary<int,Material> mMjMaterialDic = new Dictionary<int,Material>();

    public void OnLoadMJRes(Transform savelocal,Transform table)
    {
        mMJObj = ResourceManager.Instance.LoadPrefab("Hall/FBX", "mj1");
 
        for (int i = 1; i <= 61; i++)
        {
            if (OnGetMJGroupById(i) != MJGroup.None)
            {
                Material met = MaterialManager.LoadMaterial("Hall/FBX/HotMat", i.ToString()).material;
                mMjMaterialDic.Add(i,met);
                
                List<MJCS> mj = new List<MJCS>();
                for (int j = 0; j < 6; j++)
                {
                    GameObject obj = GameObject.Instantiate(mMJObj);
                    obj.name = i + "_" + j;
                    MJCS cs = obj.AddComponent<MJCS>();
                    cs.OnInit(savelocal,table,met,i,j);
                    mj.Add(cs);
                }
                mMJCardDic.Add(i,mj);
        
                Sprite sp = ResourceManager.Instance.LoadSprite("Hall/FBX/MJCardFace", i.ToString());
                mMjCardSpriteDic.Add(i,sp);
            }
        }
        
        // Debug.LogError("创建 预制体结束=========================================================================");
    }

    //获取一个麻将的mat
    public Material OnGetMaterialBymjId(int id)
    {
        if (mMjMaterialDic.ContainsKey(id))
        {
            return mMjMaterialDic[id];
        }

        return null;
    }
    
    /// <summary>
    /// 获取是属于什么组的
    /// </summary>
    /// <returns></returns>
    public MJGroup OnGetMJGroupById(int index)
    {
        if (index >= 1 && index <= 9)
            return MJGroup.Wan;
        else if (index >= 11 && index <= 19)
            return MJGroup.Tiao;
        else if (index >= 21 && index <= 29)
            return MJGroup.Bin;
        
        else if (index >= 31 && index <= 37)
            return MJGroup.Feng;
        else if (index >= 51 && index <= 61)
            return MJGroup.Hua;
        return MJGroup.None;
    }

    /// <summary>
    /// 喷射麻将 所需要的麻将牌
    /// </summary>
    /// <param name="type"> 那种形式的喷射 </param>
    public List<MJCS> OnGetNeedShowMjList(int needmjCount = 1,bool islockPos = false)
    {
        if (mMJCardDic.Count <= 0)
        {
            return null;
        }

        mShowMjList.Clear();
        // List<MJCS> needsendmj = new List<MJCS>();
        List<int> mjindexlist = new List<int>();
        foreach (var VARIABLE in mMJCardDic)
        {
            mjindexlist.Add(VARIABLE.Key);
        }
        //因为需要三个消除一个，所以*3
        int maxmjcount = needmjCount;
        if (maxmjcount > mMJCardDic.Count)
        {
            maxmjcount = mMJCardDic.Count;
        }
        
        //(10−2(n−1))*6+(4(n−1))*3
        int level1_getCount = 0;
        if (GameHelp.GameType == 1)
        {
            if (GameHelp.GameLevel >= 2 && GameHelp.GameLevel <= 5)
            {
                switch (GameHelp.GameLevel)
                {
                    case  2: level1_getCount = 2; break;
                    case  3: level1_getCount = 1; break;
                    case  4: level1_getCount = 1; break;
                    case  5: level1_getCount = 1; break;
                    // case  6: level1_getCount = 1; break;
                    // case  7: level1_getCount = 1; break;
                    // case  8: level1_getCount = 1; break;
                    // case  9: level1_getCount = 1; break;
                }
                // level1_getCount = (10 - 2 * (GameHelp.GameLevel - 1));
            }
            else if (GameHelp.GameLevel >= 7 && GameHelp.GameLevel <= 11)
            {
                level1_getCount = (8 - 2 * (GameHelp.GameLevel - 7));
            }
        }
        else
        {
            switch (GameHelp.GameLevel)
            {
                case 2: level1_getCount = 2; break;
                case 3: level1_getCount = 3; break;
                case 4: level1_getCount = 1; break;
                case 5: level1_getCount = 0; break;
                case 6: level1_getCount = 3; break;
                case 7: level1_getCount = 2; break;
                case 8: level1_getCount = 1; break;
            }
        }
        
        int curmjCount = 0;
        while (curmjCount < maxmjcount)
        {
            // Debug.LogError(curmjCount+","+maxmjcount+"进入几次："+level1_getCount);
            int randowm1 = Random.Range(0, mjindexlist.Count);
            int randowm2 = mjindexlist[randowm1];

            int getmjcount = 3;
            if (GameHelp.GameType == 1 && GameHelp.GameLevel >= 2)
            {
                if (level1_getCount >= 1)
                {
                    level1_getCount--;
                    getmjcount = 6;
                }
            }
            else if (GameHelp.GameType == 2)
            {
                if (level1_getCount >= 1)
                {
                    level1_getCount--;
                    getmjcount = 6;
                }
            }
            for (int i = 0; i < getmjcount; i++)
            {
                // mMJCardDic[randowm2][i].OnSetSale(sacle);
                mMJCardDic[randowm2][i].OnLockRightBodyPositoion(islockPos);
                // mMJCardDic[randowm2][0].OnSetLive();
                mShowMjList.Add(mMJCardDic[randowm2][i]);
            }

            mjindexlist.RemoveAt(randowm1);
            curmjCount++;
        }

        mShowMjList = mShowMjList.OrderBy(x => Random.Range(0, 1000)).ToList();
        mShowMjList = mShowMjList.OrderBy(x => Random.Range(0, 1000)).ToList();
        mShowMjList = mShowMjList.OrderBy(x => Random.Range(0, 1000)).ToList();
        // mShowMjList = needsendmj;
        return mShowMjList;
    }

    //获取实例化出来的一张牌
    public MJCS OnGetOneDisLiveMJById(int cardfaceid)
    {
        if (mShowMjList.Count > 0)
        {
            for (int i = 0; i < mShowMjList.Count; i++)
            {
                if (!mShowMjList[i].GetLiveStatus() && mShowMjList[i].mMJCardNum == cardfaceid)
                {
                    return mShowMjList[i];
                }
            }
        }

        return null;
    }
    public MJCS OnGetOneLiveMJById(int cardfaceid,int cardfaceindex)
    {
        if (mShowMjList.Count > 0)
        {
            for (int i = 0; i < mShowMjList.Count; i++)
            {
                if (mShowMjList[i].GetLiveStatus() && mShowMjList[i].mMJCardNum == cardfaceid && mShowMjList[i].mMJCardNumIndex == cardfaceindex)
                {
                    return mShowMjList[i];
                }
            }
        }

        return null;
    }
    
    //洗牌之前先删除之前的牌
    public void OnDesctroyCard()
    {
        for (int i = 0; i < mShowMjList.Count; i++)
        {
            mShowMjList[i].OnSetDisLive();
        }
    }
    
    //桌面上的牌整理一下
    public void OnZhengli()
    {
        // Transform trans = GameObject.Find("Canvas").transform;
        for (int i = 0; i < mShowMjList.Count; i++)
        {
            if (mShowMjList[i].GetLiveStatus())
            {
                mShowMjList[i].OnZhengli();
            }
        }
    }
    
    //桌面还有存活的牌
    public bool OnTableHaveLiveCard()
    {
        for (int i = 0; i < mShowMjList.Count; i++)
        {
            if (mShowMjList[i].GetLiveStatus())
            {
                return true;
            }
        }

        return false;
    }

    //洗牌
    public List<MJCS> OnXipai()
    {
        List<MJCS> livecardlist = new List<MJCS>();
        for (int i = 0; i < mShowMjList.Count; i++)
        {
            if (mShowMjList[i].GetLiveStatus())
            {
                mShowMjList[i].DoMoveZero();
                livecardlist.Add(mShowMjList[i]);
            }
        }

        return livecardlist;
    }

    /// <summary>
    /// 获取三张牌 活着的牌
    /// </summary>
    /// <returns></returns>
    public List<MJCS> OnGetLiveCardForXiaochu()
    {
        List<MJCS> livecardlist = new List<MJCS>();
        if (mShowMjList.Count > 0)
        {
            for (int i = 0; i < mShowMjList.Count; i++)
            {
                if (mShowMjList[i].GetLiveStatus())
                {
                    livecardlist.Clear();
                    int cardnumindex = mShowMjList[i].mMJCardNumIndex;
                    for (int j = 0; j < 3; j++)
                    {
                        MJCS livecard = OnGetOneLiveMJById(mShowMjList[i].mMJCardNum, cardnumindex);
                        if (livecard != null && livecard.GetLiveStatus())
                        {
                            livecardlist.Add(livecard);
                        }

                        cardnumindex++;
                        if (cardnumindex >= 3)
                        {
                            cardnumindex = 0;
                        }
                    }

                    if (livecardlist.Count >= 3)
                    {
                        break;
                    }
                }
            }
        }
        return livecardlist;
    }
    
    
    /// <summary>
    /// 磁铁一次性吸走九个牌
    /// </summary>
    /// <returns></returns>
    public List<MJCS> OnGetLiveCardForCitie()
    {
        List<MJCS> livecardlist = new List<MJCS>();
        List<MJCS> threecardlist = new List<MJCS>();
        if (mShowMjList.Count > 0)
        {
            for (int i = 0; i < mShowMjList.Count; i++)
            {
                if (mShowMjList[i].GetLiveStatus())
                {
                    int cardnum = mShowMjList[i].mMJCardNum;
                    int cardnumindex = mShowMjList[i].mMJCardNumIndex;
                    threecardlist.Clear();

                    bool livelisthavecardnum = false;
                    for (int j = 0; j < livecardlist.Count; j++)
                    {
                        if (livecardlist[j].mMJCardNum == cardnum)
                        {
                            livelisthavecardnum = true;
                        }
                    }

                    //如果加入集合过了的，for循环再次循环起来
                    if (livelisthavecardnum)
                    {
                        continue;
                    }
                    
                    for (int j = 0; j < 3; j++)
                    {
                        MJCS livecard = OnGetOneLiveMJById(cardnum, cardnumindex);
                        if (livecard != null && livecard.GetLiveStatus())
                        {
                            threecardlist.Add(livecard);
                        }

                        cardnumindex++;
                        if (cardnumindex >= 3)
                        {
                            cardnumindex = 0;
                        }
                    }
                    
                    if (threecardlist.Count >= 3)
                    {
                        for (int j = 0; j < threecardlist.Count; j++)
                        {
                            livecardlist.Add(threecardlist[j]);
                        }

                        if (livecardlist.Count >= 9)
                        {
                            break;
                        }
                    }
                }
            }
        }
        return livecardlist;
    }

    
    
    /// <summary>
    /// 初始化数据
    /// </summary>
    public override void InitData()
    {
    }

    /// <summary>
    /// 清理数据
    /// </summary>
    public override void ClearData()
    {
    }
}
